package com.itheima.smspringboot.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.smspringboot.entity.SchoolClass;
import com.itheima.smspringboot.entity.Teacher;
import com.itheima.smspringboot.mapper.SchoolClassMapper;
import com.itheima.smspringboot.mapper.TeacherMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {


    @Resource
    private TeacherMapper teacherMapper;

    @Autowired
    private SchoolClassMapper schoolClassMapper;

    public  void deleteBatch(List<Teacher> teacherList) {
        for (Teacher teacher : teacherList) {
            teacherMapper.deleteById(teacher.getTeacId());
        }
    }

    public  void deleteById(Integer teacId) {
        SchoolClass schoolClass = schoolClassMapper.selectById(teacId);
        if (schoolClass != null) {
            schoolClass.setClassAdvisor(null);
            schoolClassMapper.updateById(schoolClass);
        }

        teacherMapper.deleteById(teacId);

    }

    public  void updateById(Teacher teacher) {
        teacherMapper.updateById(teacher);
    }

    public List<Teacher> selectAll(Teacher teacher) {
        return teacherMapper.selectAll(teacher);
    }



    public PageInfo selectPage(Integer pageNum, Integer pageSize, Teacher teacher) {
        //H=开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> list = teacherMapper.selectAll(teacher);
        return PageInfo.of(list);

    }

    public void add(Teacher teacher) {
        teacherMapper.insert(teacher);
    }

}
